//-----------------------------------------------------------------------
// <copyright file="a_CatMainDAOBase.cs" company="AACECOM">
//     Copyright (c) AACECOM. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ThoiTrang.Entities;

namespace ThoiTrang.Data
{    
    public abstract class a_CatMainDAOBase
    {
        #region Common methods
        public virtual a_CatMain Createa_CatMainFromReader(IDataReader reader)
        {
            a_CatMain item = new a_CatMain();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("CatID"))) item.CatID = (int)reader["CatID"];
                if (!reader.IsDBNull(reader.GetOrdinal("UName"))) item.UName = (string)reader["UName"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("GroupID"))) item.GroupID = (int)reader["GroupID"];
            }
            catch (Exception ex)
            {                
                throw ex;
            }
            return item;
        }
        #endregion
        
        #region Createa_CatMain methods
            
        public virtual void Createa_CatMain(a_CatMain a_CatMain)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatMain_ins");
                
                database.AddInParameter(dbCommand, "@UName", DbType.String, a_CatMain.UName);
                database.AddInParameter(dbCommand, "@Name", DbType.AnsiString, a_CatMain.Name);
                database.AddInParameter(dbCommand, "@GroupID", DbType.Int32, a_CatMain.GroupID);
                database.AddOutParameter(dbCommand, "@CatID", DbType.Int32, 0);
                
                database.ExecuteNonQuery(dbCommand);
                a_CatMain.CatID = (int)database.GetParameterValue(dbCommand, "@CatID");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region Updatea_CatMain methods
        
        public virtual void Updatea_CatMain(a_CatMain a_CatMain)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatMain_upd");            
                
                database.AddInParameter(dbCommand, "@CatID", DbType.Int32, a_CatMain.CatID);
                database.AddInParameter(dbCommand, "@UName", DbType.String, a_CatMain.UName);
                database.AddInParameter(dbCommand, "@Name", DbType.AnsiString, a_CatMain.Name);
                database.AddInParameter(dbCommand, "@GroupID", DbType.Int32, a_CatMain.GroupID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }
        
        #endregion

        #region Deletea_CatMain methods
        public virtual void Deletea_CatMain(int catID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatMain_del");
                
                database.AddInParameter(dbCommand, "@CatID", DbType.Int32, catID);
                
                database.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
               throw ex;
            }
        }

        #endregion

        #region Geta_CatMain methods
        
        public virtual a_CatMain Geta_CatMain(int catID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatMain_sel");
                
                database.AddInParameter(dbCommand, "@CatID", DbType.Int32, catID);
                
                a_CatMain a_CatMain =  null;
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    if (reader.Read())
                    {
                        a_CatMain = Createa_CatMainFromReader(reader);
                        reader.Close();
                    }
                }
                return a_CatMain;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        #endregion

        #region Geta_CatMainList methods
        public virtual a_CatMainCollection Geta_CatMainList(a_CatMainColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("a_CatMain_selList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                a_CatMainCollection a_CatMainCollection = new a_CatMainCollection();
                using (IDataReader reader = database.ExecuteReader(dbCommand))
                {
                    while (reader.Read())
                    {
                        a_CatMain a_CatMain = Createa_CatMainFromReader(reader);
                        a_CatMainCollection.Add(a_CatMain);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return a_CatMainCollection;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        
        public virtual a_CatMainCollection Geta_CatMainList(a_CatMainColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return Geta_CatMainList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
    }
}